﻿//using System;
//using System.Data;
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using FutureOpen.Web.Pages;
using FutureOpen.Core;
using FutureOpen.Core.func;
using FutureOpen.Core.func.db;
using FutureOpen.Core.func.util;
using System.Drawing.Imaging;
using System.IO;

public partial class OrgUpdate : PageBase
{
    private byte[] ImgBytes = null;
    private const int StdImgSize = 120;
    protected void Page_Load(object sender, EventArgs e)
    {
        base.PageInit();
        if (!Page.IsPostBack)
        {
            //SimulateLogin();
            //Response.Write(this.MyUsr.Usr_email);
            string Org_ID = Request.QueryString["id"];  
            MyOrg objMyOrg = new MyOrg();
            Org objOrg = null;
            if (Org_ID != null) objOrg = objMyOrg.GetOrgByID(int.Parse(Org_ID));
            Load_Type();
            Load_Category();
            if (objOrg != null)
            {
                objOrg = objMyOrg.GetOrgByID(int.Parse(Org_ID));
                this.TextBox_OrgName.Text = objOrg.Org_name;
                this.TextBox_OrgDesc.Text = objOrg.Org_desc;
                this.Image1.ImageUrl = "GetImageOrg.ashx?orgid=" + objOrg.Org_id.ToString();
                
                foreach (ListItem li in this.DropDownList_OrgCategory.Items)
                {
                    if (li.Value == objOrg.Org_cat.ToString())
                    {
                        li.Selected = true;
                        break;
                    }
                }
                foreach (ListItem li in this.DropDownList_OrgType.Items)
                {
                    if (li.Value == objOrg.Org_type.ToString())
                    {
                        li.Selected = true;
                        break;
                    }
                }
            }
        }
    }
    protected void Load_Type()
    {
        /*
        if (this.MyUsr != null && this.MyUsr.Usr_type == UsrType.Usrtype_Admin)
        {
            if (DropDownList_OrgType.Items.FindByText("自己创建的组织") == null)
            {
                DropDownList_OrgType.Items.Add(new ListItem("自己创建的组织", "13"));
            }
        }*/
    }
    protected void Load_Category()
    {
        MyCat objMyCat = new MyCat();
        IList<Cat> lcats = objMyCat.GetChildren(CatType.CATTYPE_ROOT);
        this.DropDownList_OrgCategory.Items.Clear();
        foreach (Cat c in lcats)
        {
            this.DropDownList_OrgCategory.Items.Add(new ListItem(c.Cat_name, c.Cat_id.ToString()));
        }
    }

    public bool ThumbnailCallback()
    {
        return false;
    }

    private void GetThumb()
    {
        string imgContentType = FileUpload_OrgImg.PostedFile.ContentType;
        bool IsJpeg = (FileUpload_OrgImg.PostedFile.FileName.ToLower().EndsWith(".jpg") || FileUpload_OrgImg.PostedFile.FileName.ToLower().EndsWith(".jpeg"));
        System.Drawing.Image JpgImg = null;
        System.Drawing.Image OrigImg = null;
        System.Drawing.Image ThumbImg = null;
        MemoryStream ms = new MemoryStream();
        MemoryStream Thumbms = new MemoryStream();
        if (FileUpload_OrgImg.PostedFile.ContentType.ToLower().StartsWith("image"))
        {
            OrigImg = System.Drawing.Image.FromStream(FileUpload_OrgImg.PostedFile.InputStream);
            try
            {
                if (IsJpeg)
                {
                    JpgImg = OrigImg;
                }
                else
                {
                    OrigImg.Save(ms, ImageFormat.Jpeg);
                    JpgImg = System.Drawing.Image.FromStream(ms);
                }

                ThumbImg = JpgImg.GetThumbnailImage(StdImgSize, StdImgSize, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
                ThumbImg.Save(Thumbms, ImageFormat.Jpeg);
                ImgBytes = Thumbms.GetBuffer();
            }
            finally
            {
                if (OrigImg != null) OrigImg.Dispose();
                if (JpgImg != null) JpgImg.Dispose();
                if (ThumbImg != null) ThumbImg.Dispose();
                if (ms != null) ms.Close();
                if (Thumbms != null) Thumbms.Close();
            }
        }
    }

    protected void Button_OrgCreate_Click(object sender, EventArgs e)
    {
        try
        {
            string Org_ID = Request.QueryString["id"];
            Org objOrg = new Org();
            MyOrg objMyOrg = new MyOrg();
            objOrg = objMyOrg.GetOrgByID(int.Parse(Org_ID));

            if (this.FileUpload_OrgImg.FileName != string.Empty)
            {
                GetThumb();
                if (ImgBytes != null)
                {   
                    objOrg.Org_logo_small = ImgBytes;
                }
                else
                {
                    this.Label_Result.Text = "无法解析图片";
                    return;
                }
                
            }
            objOrg.Org_prefix = StringUtil.ChineseCap(TextBox_OrgName.Text);
            objOrg.Org_name = this.TextBox_OrgName.Text;
            objOrg.Org_desc = this.TextBox_OrgDesc.Text;
            objOrg.Org_type = short.Parse(this.DropDownList_OrgType.SelectedItem.Value);
            objOrg.Org_cat = int.Parse(this.DropDownList_OrgCategory.SelectedItem.Value);
            int code = objMyOrg.UpdateOrg(objOrg);
            switch (code)
            {
                case StateCode.UPDATEORG_OK:
                    Label_Result.Text = "组织更新成功!";
                    Response.Redirect("OrgView.aspx?id=" + objOrg.Org_id.ToString());
                    break;
                case StateCode.UPDATEORG_FAIL:
                    Label_Result.Text = "组织更新失败!";
                    break;
                default: break;
            }
        }
        finally
        {
            ImgBytes = null;           
        }
    }

}
